package com.example.springboot1.utils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

/**
 * @author ckj
 * @description TODO
 * @date 2023-05-06 17:25
 */


public class JsUtil {
    public static void main(String[] args) {
        ScriptEngineManager sem = new ScriptEngineManager();
        //查找并为给定的扩展创建ScriptEngine。也可用getEngineByName，查找并为给定名称创建ScriptEngine
        ScriptEngine jsEngine = sem.getEngineByExtension("js");
        try {
            jsEngine.eval("var array = [1, 2, 3, 4, 5];for (var i = 0; i < array.length; i++) {print('index:' + i + ',value:' + array[i]);}");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
